﻿using ExpenseSystem.Core.CustomerModules;
using ExpenseSystem.Core.DataAddOn;
using ExpenseSystem.Core.OrderModules;
using ExpenseSystem.Fether;
using ExpenseSystem.Model;
using ExpenseSystem.Common;

namespace ExpenseSystem.Core.BonusModules
{
    public class CustomerOtherUpdCusManager : CustomerOtherManager
    {
        CustomerOtherManager orgDbManager;
        CustomerManager customerManager = new CustomerBaseManager();
        public CustomerOtherUpdCusManager(CustomerOtherManager coManager)
        {
            this.orgDbManager = coManager;
        }
        private bool UpdateCustomer(long site, long cusId, decimal amount, out string message)
        {
            bool result = false;
            CustomerFetcher customerFetcher = new CustomerFetcher();
            CustomerModel customerInfo = customerFetcher.Get(site, cusId, out message);
            if (customerInfo == null)
            {
                LogHelper.Error(this.GetType().FullName,"无法找到对应的客户信息");
                return false;
            }
            customerInfo.OtherBonus += amount;
            result = customerManager.Save(customerInfo, out message);
            if(result)
            {
                LogHelper.Info(this.GetType().FullName, "更新客户累计设计费用信息成功");
            }
            else
            {
                LogHelper.Error(this.GetType().FullName, string.Format("更新客户累计设计费用信息失败:{0}",message));
            }
            return result;
        }
        public override bool Approved(long id, out string message)
        {
            CustomerOtherFetcher dbFetcher = new CustomerOtherFetcher();
            CustomerOtherModel dbModel;
            decimal amount = 0;
            if (id == 0)
            {
                message = "传入的参数有误";
                return false;
            }
            else
            {
                dbModel = dbFetcher.Get(profile.SiteId, id, out message);
                if (dbModel == null)
                {
                    return false;
                }
                amount += dbModel.Amount;
                
            }
            bool result = UpdateCustomer(dbModel.Site, dbModel.CustomerId, amount, out message);
            if(result)
                result = orgDbManager.Approved(id, out message);
            return result;
        }
        public override bool Revert(long modelid, out string message)
        {
            CustomerOtherFetcher dbFetcher = new CustomerOtherFetcher();
            CustomerOtherModel dbModel;
            decimal amount = 0;
            if (modelid == 0)
            {
                message = "传入的参数有误";
                return false;
            }
            else
            {
                dbModel = dbFetcher.Get(profile.SiteId, modelid, out message);
                if (dbModel == null)
                    return false;
                amount -= dbModel.Amount;
            }
            bool result = UpdateCustomer(dbModel.Site, dbModel.CustomerId, amount, out message);
            if (result)
                result = orgDbManager.Revert(modelid, out message);

            return result;
        }


        public override bool Delete(CustomerOtherModel dbm, out string message)
        {
                return orgDbManager.Delete(dbm, out message);

        }

        public override bool DeleteById(long[] PayId, out string message)
        {
            return orgDbManager.DeleteById(PayId, out message);
        }

        public override bool Save(CustomerOtherModel dbmodel, out string message, params IAddable<CustomerOtherModel>[] addables)
        {
            return orgDbManager.Save(dbmodel, out message, addables);
        }
    }
}
